
var all_sss = [];

SignStatSession = function() {
	this.id = null;
	this.url = null;
	this.from = 0;
	this.to = 0;
	this.top_cnt = 0;
	this.unique_words = []; // posts
	this.sign_stat = []; // user sign stat
	this.session = null;
	this.multi_sign = [];
	this.copy_words = [];
	this.post_url = null;
	this.stop = true;
}

SignStatSession.prototype.save = function() {
	for (var i in all_sss) {
		var sss = all_sss[i];
		if (sss.id == this.id) {
			all_sss.splice(i, 1);
			break;
		}
	}
	all_sss.push(this);
	localStorage.sss = JSON.stringify(all_sss);
}

SignStatSession.prototype.clone = function(obj) {
	if (obj.id == null || obj.from == null || obj.url == null) {
		error("invalid sss");
		error(JSON.stringify(obj));
		return;
	}
	this.id = obj.id;
	this.url = obj.url;
	this.from = obj.from;
	this.to = obj.to;
	this.top_cnt = obj.top_cnt;
	this.post_url = obj.post_url;
	if (obj.unique_words)
		this.unique_words = obj.unique_words;
	if (obj.sign_stat)
		this.sign_stat = obj.sign_stat;
	if (obj.session) {
		this.session = new Session(sss_session_finished);
		this.session.sss_id = this.id;
		this.session.clone(obj.session);
		this.session.detail = true;
	}
	if (obj.multi_sign)
		this.multi_sign = obj.multi_sign;
	if (obj.copy_words)
		this.copy_words = obj.copy_words;
}

SignStatSession.prototype.start = function() {
	if (this.session == null) {
		this.session = new Session(sss_session_finished);
		this.session.detail = true;
		this.session.sss_id = this.id;
		this.session.type = 'time';
		this.session.from = this.from;
		this.session.to = this.session.from + 24*3600*1000;
		this.session.url = this.url;
		this.session.pending.push(append_prefix(this.url));
	}
	this.stop = false;
	start(this.session, true, 0);
}

SignStatSession.prototype.addNewSession = function(session) {
	for (var i in session.results) {
		var ups = session.results[i];
		if (ups.count > 1 && ups.id != '0' && false) { // id == 0 means the user is deleted, no need to calc multi sign
			// sign multiple times
			ups.date = session.from;
			var duplicate = false;
			for (var j in this.multi_sign) {
				var ups_ms = this.multi_sign[j];
				if (ups_ms.uid == ups.uid && ups_ms.date == ups.date) {
					duplicate = true;
				}
			}
			if (!duplicate) 
				this.multi_sign.push(ups);
		}
		var uss = this.getUserSignStat(ups.uid);
		if (uss == null) {
			uss = new UserSignStat();
			uss.id = ups.id;
			uss.uid = ups.uid;
			uss.count = 1;
			uss.last_sign_date = session.from;
			this.sign_stat.push(uss);
		} else {
			uss.count++;
			uss.last_sign_date = session.from;
		}
	}
	this.session = session;
	this.save();

	// check copy post
	for (var i in session.thread.pages) {
		var page = session.thread.pages[i];
		for (var j in page.posts) {
			var post = page.posts[j];
			extract_word(post);
			this.checkCopy(post);
		}
	}
	
	if (this.multi_sign.length > 100) this.stop = true;
	if (this.copy_words.length > 100) this.stop = true;
	if (!this.stop) {
		var new_session = create_new_session(session);
		new_session.sss_id = this.id;
		new_session.detail = true;
		start(new_session, true, 0);
	}
}

SignStatSession.prototype.getUserSignStat = function(uid) {
	for (var i in this.sign_stat) {
		var uss = this.sign_stat[i];
		if (uss.uid == uid)
			return uss;
	}
	return null;
}

SignStatSession.prototype.checkCopy = function(post) {
	return; // do not need to check
	for (var i in this.unique_words) {
		var up = this.unique_words[i];
		if (up.content == post.content && up.id != post.id && up.uid != post.uid) {
			post.copy = up;	
			post.date = this.session.from;
			this.copy_words.push(post);
			return;
		}
	}
	this.unique_words.push(post);
	if (this.unique_words.length > 100) {
		this.unique_words.shift();
	}
}

function sss_session_finished(session) {
	var sss = get_sss(session.sss_id);
	sss.addNewSession(session);
}

function start_sss(mm) {
	var sss = get_sss(mm.id);
	if (sss == null) {
		var from = document.getElementById("ss_from_" + mm.id).value;
		var to = document.getElementById("ss_to_" + mm.id).value;
		var top_cnt = document.getElementById("ss_topcnt_" + mm.id).value;
		if (from == '' || to == '' || top_cnt == '') {
			error("Please specify condition");
			return false;
		}
		sss = new SignStatSession();
		sss.id = mm.id;
		sss.from = Date.parse(from);
		sss.to = Date.parse(to);
		sss.top_cnt = parseInt(top_cnt, 10);
		sss.url = mm.url;
		sss.save();
	}
	sss.start();
	return true;
}

function stop_sss(mm) {
	var sss = get_sss(mm.id);
	sss.stop = true;
	nest.stop();
}

function save_sss() {
	var mm_id = event.srcElement.mm_id;
	if (mm_id == null) return;
	var sss = get_sss(mm_id);
	var from = document.getElementById("ss_from_" + mm_id).value;
	var to = document.getElementById("ss_to_" + mm_id).value;
	var top_cnt = document.getElementById("ss_topcnt_" + mm_id).value;
	var post_url = document.getElementById("ss_post_url_" + mm_id).value;
	if (from == '' || to == '' || top_cnt == '') return;
	sss.from = Date.parse(from);
	sss.to = Date.parse(to);
	sss.top_cnt = parseInt(top_cnt, 10);
	if (post_url)
		sss.post_url = remove_prefix(post_url);
	sss.save();
}

function init_toolbar_ss(mm, toolbar) {
	var start = document.createElement("input");
	start.type = 'button';
	start.value = 'Start';
	start.mm = mm;
	start.onclick = stat_mm;
	toolbar.appendChild(start);
	var label = document.createElement("label");
	label.style.paddingLeft = '10px';
	label.innerText = 'From:';
	toolbar.appendChild(label);
	var from = document.createElement("input");
	from.id = 'ss_from_' + mm.id;
	from.mm_id = mm.id;
	from.type = 'datetime';
	from.size = 20;
	from.onchange = save_sss;
	toolbar.appendChild(from);
	label = document.createElement("label");
	label.innerText = 'To:';
	toolbar.appendChild(label);
	var to = document.createElement("input");
	to.id = 'ss_to_' + mm.id;
	to.mm_id = mm.id;
	to.type = 'datetime';
	to.size = 20;
	to.onchange = save_sss;
	toolbar.appendChild(to);
	label = document.createElement("label");
	label.style.paddingLeft = '5px';
	label.innerText = 'Display Top:';
	toolbar.appendChild(label);
	var topcnt = document.createElement("input");
	topcnt.id = 'ss_topcnt_' + mm.id;
	topcnt.mm_id = mm.id;
	topcnt.type = 'number';
	topcnt.size = 10;
	topcnt.onchange = save_sss;
	toolbar.appendChild(topcnt);
	var a = document.createElement("a");
	a.href = append_prefix(mm.url);
	a.innerText = 'Link';
	a.target = '_blank';
	toolbar.appendChild(a);
	label = document.createElement("label");
	label.style.paddingLeft = '5px';
	label.innerText = 'Post URL:';
	toolbar.appendChild(label);
	var posturl = document.createElement("input");
	posturl.id = 'ss_post_url_' + mm.id;
	posturl.mm_id = mm.id;
	posturl.type = 'url';
	posturl.size = 30;
	posturl.onchange = save_sss;
	toolbar.appendChild(posturl);
	var postbtn = document.createElement("input");
	postbtn.id = 'ss_post_result_' + mm.id;
	postbtn.mm_id = mm.id;
	postbtn.type = 'button';
	postbtn.value = 'Post';
	postbtn.onclick = post_sss;

	display_sss(mm);
}

function display_ss_result(sss) {
	var div = get_current_post_area();
	if (sss.session == null) return;
	div.innerHTML = "";
	sss.sign_stat.sort(sort_ups);
	var max = calc_max_width(sss.sign_stat);
	var result = document.createElement("div");
	div.appendChild(result);
	result.id = 'sss_result_' + sss.id;
	for (var i in sss.sign_stat) {
		var r = sss.sign_stat[i];
		result.innerHTML += 'ID: ' + pad(r.id, max) + '   签到: ' + r.count + '次<br/>';
		if (i > sss.top_cnt) {
			break;
		}
	}
	var label = document.createElement("label");
	label.innerText = "多次签到";
	label.style.textWeight = "bold";
	div.appendChild(label);
	var post_ms = document.createElement("input");
	post_ms.id = 'post_ms_' + sss.id;
	post_ms.type = 'button';
	post_ms.value = 'Post';
	post_ms.sss = sss;
	div.appendChild(post_ms);
	var del_ms = document.createElement("input");
	del_ms.id = 'del_ms_' + sss.id;
	del_ms.type = 'button';
	del_ms.value = 'Delete';
	del_ms.sss = sss;
	div.appendChild(del_ms);
	var div_ms = document.createElement("div");
	div_ms.id = 'multi_sign_' + sss.id;
	div.appendChild(div_ms);
	for (var i in sss.multi_sign) {
		var ups = sss.multi_sign[i];
		div_ms.innerHTML += 'ID: ' + pad(ups.id, max) + ' ' + display_date_result(new Date(ups.date)) + '签到' + ups.count + '次:<br/>';
		for (var j in ups.posts) {
			div_ms.innerHTML += "[bbs]" + add_link(ups.posts[j].url, remove_prefix(ups.posts[j].url)) + '[/bbs]<br/>';
		}
		div_ms.innerHTML += "<br/>";
	}
	label = document.createElement("label");
	label.innerText = "复制回复";
	label.style.textWeight = "bold";
	div.appendChild(label);
	var post_cw = document.createElement("input");
	post_cw.id = 'post_cw_' + sss.id;
	post_cw.type = 'button';
	post_cw.value = 'Post';
	post_cw.sss = sss;
	div.appendChild(post_cw);
	div.innerHTML += "&nbsp;";
	var del_cw = document.createElement("input");
	del_cw.id = 'del_cw_' + sss.id;
	del_cw.type = 'button';
	del_cw.value = 'Delete';
	del_cw.sss = sss;
	div.appendChild(del_cw);
	var div_cw = document.createElement("div");
	div_cw.id = 'copy_words_' + sss.id;
	for (var i in sss.copy_words) {
		var post = sss.copy_words[i];
		div_cw.innerHTML += 'ID: ' + pad(post.id, max) + '在 ' + add_link(post.url, post.floor) + ' 楼复制 ' + add_link(post.copy.url, post.copy.floor) + ' 楼回复&nbsp:' + post.content + '<br/>';
	}
	post_ms = document.getElementById("post_ms_" + sss.id);
	del_ms = document.getElementById("del_ms_" + sss.id);
	post_cw = document.getElementById("post_cw_" + sss.id);
	del_cw = document.getElementById("del_cw_" + sss.id);
	post_ms.onclick = post_multi_sign;
	post_ms.sss = sss;
	del_ms.onclick = delete_multi_sign;
	del_ms.sss = sss;
	post_cw.onclick = post_copy_words;
	post_cw.sss = sss;
	del_cw.onclick = delete_copy_words;
	del_cw.sss = sss;
}

function post_sss() {
	var sss = get_sss(event.srcElement.mm_id);
	var txt = document.getElementById("sss_result_" + sss.id).innerText;
	post_content(append_prefix(sss.post_url), txt);	
}

function post_multi_sign() {
	var sss = event.srcElement.sss;
	var txt = document.getElementById("multi_sign_" + sss.id).innerText;
	post_content(append_prefix(sss.post_url), txt);	
}

function post_copy_words() {
	var sss = event.srcElement.sss;
	var txt = document.getElementById("copy_words_" + sss.id).innerText;
	post_content(append_prefix(sss.post_url), txt);	
}

function delete_multi_sign() {
	var sss = event.srcElement.sss; 
	sss.multi_sign = new Array();
	sss.save();
	display_ss_result(sss);
}

function delete_copy_words() {
	var sss = event.srcElement.sss; 
	sss.copy_words = new Array();
	sss.save();
	display_ss_result(sss);
}

function add_link(url, txt) {
	if (url == null) return '';
	if (txt == null) txt = 'Link';
	return '<a href="' + append_prefix(url) + '" target="_blank">' + txt + '</a>';
}

function display_sss(mm) {
	var sss = get_sss(mm.id);
	if (sss == null) return;
	var from = document.getElementById("ss_from_" + mm.id);
	var to = document.getElementById("ss_to_" + mm.id);
	var top_cnt = document.getElementById("ss_topcnt_" + mm.id);
	var post_url = document.getElementById("ss_post_url_" + mm.id);
	from.value = display_date(new Date(sss.from));
	to.value = display_date(new Date(sss.to));
	top_cnt.value = sss.top_cnt;
	post_url.value = sss.post_url;
	display_ss_result(sss);
}

function get_sss(id) {
	for (var i in all_sss) {
		var sss = all_sss[i];
		if (sss.id == id) {
			return sss;
		}
	}
	return null;
}

function extract_word(post) {
	post.content = ''; // copy word is not counted
	return;
	var flag = remove_html_tags(post);
	if (!flag) return;
	var pos = post.content.indexOf('每日心语');
	var tmp = post.content.substring(pos + 5);
	var end = tmp.indexOf('<');
	if (end > 0)
		tmp = tmp.substring(0, end);
	post.content = tmp;
}


function load_sss() {
	if (all_sss.length == 0) {
		var str = localStorage.sss;
		if (str == null || str == '') return new Array();
		var ary = JSON.parse(str);
		for (var i in ary) {
			var obj = ary[i];
			var sss = new SignStatSession();
			sss.clone(obj);
			all_sss.push(sss);
		}
	}
}

load_sss();
